package control
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

    public class ScaleBound extends Sprite
    {
        private var markers: Array = []
        private var markerCount: int = 8
        private var label: Caption
        
        protected var _rect: Rectangle
        
        /**
         * Constructor
         * @param rect = rectangle of selected area
         * @param markerCount = Count of markers in bound. May be 4 or 8
         */ 
        public function ScaleBound(rect:Rectangle=null, markerCount:int=8) 
        {
            super();
            this._rect = rect;
            this.markerCount = markerCount;
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        /** 
         * init 
         */
        protected function init (event:Event=null): void {
            
            if (markers.length == markerCount) 
				return;
            
            trace(prototype + ".init()");
            
            switch (markerCount) {
                case 8:
                    markers.push(new ScaleBoundMarker(ScaleMarkerType.TOP))
                    markers.push(new ScaleBoundMarker(ScaleMarkerType.LEFT))
                    markers.push(new ScaleBoundMarker(ScaleMarkerType.RIGHT))
                    markers.push(new ScaleBoundMarker(ScaleMarkerType.BOTTOM))
                case 4:
                    markers.unshift(new ScaleBoundMarker(ScaleMarkerType.RIGHT_BOTTOM))
                    markers[0].addEventListener(ScaleBoundEvent.CHANGE, onMoveRBMarker)
                    markers.unshift(new ScaleBoundMarker(ScaleMarkerType.LEFT_BOTTOM))
                    markers[0].addEventListener(ScaleBoundEvent.CHANGE, onMoveLBMarker)
                    markers.unshift(new ScaleBoundMarker(ScaleMarkerType.RIGHT_TOP))
                    markers[0].addEventListener(ScaleBoundEvent.CHANGE, onMoveRTMarker)
                    markers.unshift(new ScaleBoundMarker(ScaleMarkerType.LEFT_TOP))
                    markers[0].addEventListener(ScaleBoundEvent.CHANGE, onMoveLTMarker)
                    break
                default:
                    throw ("Marker coun possess the value 4, 8");
                    break;
            }
            
            for (var i:int=0; i < markers.length; i++) {
                markers[i].addEventListener(ScaleBoundEvent.CHANGE, onMoveMarker)
                markers[i].addEventListener(ScaleBoundEvent.SELECT, onSelectMarker)
                markers[i].addEventListener(ScaleBoundEvent.DESELECT, onDeselectMarker)
            }
            
            drawBound()
            
            label = new Caption(_rect)
            addChild(label)
        }
      
      
        /**  **/
        protected function updateLabelPos(): void 
        {
            if (label.width > _rect.width || label.height > _rect.height)
                label.visible = false
            else
                label.visible = true
            label.text = " " + _rect.width.toFixed(0) + "x" + _rect.height.toFixed(0) + " "
            label.rect = _rect
        }
        
        
        /**  **/
        protected function drawBound (): void {
            placeBaseMarker()
            graphics.clear()
            graphics.lineStyle(1, 0xFF0000, 1, true) 
            graphics.drawRect(_rect.x, _rect.y, _rect.width, _rect.height)
            graphics.lineStyle()
        }
        
        
        /**  **/
        private function placeBaseMarker(): void {
            markers[0].x = _rect.x
            markers[0].y = _rect.y
            addChild(markers[0])
         
            markers[1].x = _rect.x + _rect.width
            markers[1].y = _rect.y
            addChild(markers[1])
         
            markers[2].x = _rect.x
            markers[2].y = _rect.y + _rect.height
            addChild(markers[2])
         
            markers[3].x = _rect.x + _rect.width
            markers[3].y = _rect.y + _rect.height
            addChild(markers[3]);
			
			
        }
        
        
        /**  **/
        private function onMoveLTMarker (event:ScaleBoundEvent): void {
            _rect.x += event.dx
            _rect.y += event.dy
            _rect.width -= event.dx
            _rect.height -= event.dy
        }
        
        
        /**  **/
        private function onMoveRTMarker (event:ScaleBoundEvent): void {
            _rect.width += event.dx
            _rect.height -= event.dy
            _rect.y += event.dy
        }
        
        
        /**  **/
        private function onMoveRBMarker (event:ScaleBoundEvent): void {
            _rect.width += event.dx
            _rect.height += event.dy
        }
        
        
        /**  **/
        private function onMoveLBMarker (event:ScaleBoundEvent): void {
            trace("onMoveLBMarker = " + event.dx + " x " + event.dy)
            _rect.x += event.dx
            _rect.width -= event.dx
            _rect.height += event.dy
        }
        
        
        /**  **/
        private function onSelectMarker (event:ScaleBoundEvent): void {
            label.visible = true
        }
            
        
        /**  **/
        private function onDeselectMarker (event:ScaleBoundEvent): void {
            label.visible = false         
        }
        
        
        /**  **/
        private function onMoveMarker (event:ScaleBoundEvent): void {
            drawBound() 
            updateLabelPos()
            dispatchEvent(event)
        }
        
        
        /**  **/
        public function set rect (value:Rectangle): void {
            _rect = value
            drawBound()
        }
        
        
        /**  **/
        public function get rect (): Rectangle {
            return _rect
        }
      
   }
}